package com.shiyanlou.upms.system.controller;

import com.shiyanlou.upms.common.annotation.Log;
import com.shiyanlou.upms.common.utils.MD5Utils;
import com.shiyanlou.upms.common.utils.PageUtils;
import com.shiyanlou.upms.common.utils.Query;
import com.shiyanlou.upms.common.utils.R;
import com.shiyanlou.upms.system.domain.UpmsUser;
import com.shiyanlou.upms.system.service.UpmsRoleService;
import com.shiyanlou.upms.system.service.UpmsUserService;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// 需要导入的包
import com.shiyanlou.upms.common.utils.MD5Utils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.web.bind.annotation.*;
// 新导入的包
import com.shiyanlou.upms.common.annotation.Log;

// 新导入的包
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@Api(tags = "用户管理")
@Controller
@RequestMapping("/user")
public class UpmsUserController {

    @Autowired
    private UpmsUserService upmsUserService;

    @Autowired
    private UpmsRoleService upmsRoleService;

    @GetMapping("/person")
    public String show(Model model) {
        UpmsUser user = new UpmsUser();
        user.setUserId(123456789);
        user.setUsername("实验楼");
        user.setPassword("123456");
        user.setCtime(new Date());
        model.addAttribute("user", user);
        return "user/person";
    }

    //通过 ID 查找用户信息
    @ApiOperation("通过ID查找用户信息")
    @GetMapping("/search")
    @ResponseBody
    public R search(Integer userId) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        UpmsUser user = upmsUserService.get(userId);
        resultMap.put("user", user);
        return R.ok(resultMap);

    }
    /**
     * 用户首页
     */
    @ApiOperation("用户管理主页")
    @Log("用户管理页面")  //日志记录功能，所有用户访问 main时，系统都会记录下用户的操作信息。
    @GetMapping("/main")
    String UpmsUser(){
        return "user/main";
    }
    /**
     * 分页查询
     */
    @Log("分页查询")    //日志记录功能，所有用户访问 list时，系统都会记录下用户的操作信息。
    @ApiOperation("用户信息分页列表")
    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        int total = upmsUserService.count(query);
        List<UpmsUser> upmsUserList = upmsUserService.list(query);
        PageUtils pageUtils = new PageUtils(upmsUserList, total);
        return pageUtils;
    }
    /**
     * 调整到新增页面
     * @return
     */
    @ApiOperation("新增用户页面")
    @Log("调整到新增页面") //日志记录功能，所有用户添加时，系统都会记录下用户的操作信息。
    @GetMapping("/add")
    String add(){
        return "user/add";
    }
    /**
     * 新增用户
     * @param upmsUser
     * @return
     */
    @ApiOperation(value="商品新增")
    @ResponseBody
    @PostMapping("/save")
    public R save( UpmsUser upmsUser){
        String salt = RandomStringUtils.randomAlphanumeric(30);
        upmsUser.setSalt(salt);
        String password = upmsUser.getPassword() + salt;
        upmsUser.setPassword(MD5Utils.encrypt(upmsUser.getUsername(), password));
        if(upmsUserService.insert(upmsUser)>0){
            return R.ok();
        }
        return R.error();
    }
    /**
     * 修改用户页面
     */
    @ApiOperation(value="修改用户页面")
    @GetMapping("/edit/{userId}")
    String edit(@PathVariable("userId") Integer userId,Model model){
        UpmsUser upmsUser = upmsUserService.get(userId);
        model.addAttribute("upmsUser", upmsUser);
        return "user/edit";
    }
    /**
     * 修改用户
     * @param upmsUser
     * @return
     */
    @ApiOperation(value="修改用户")
    @ResponseBody
    @RequestMapping("/update")
    public R update( UpmsUser upmsUser){
        upmsUserService.updateByPrimaryKeySelective(upmsUser);
        return R.ok();
    }
    /**
     * 删除用户
     * @param userId
     * @return
     */
    @ApiOperation(value="删除用户")
    @PostMapping( "/remove")
    @ResponseBody
    public R remove(Integer userId){
        if(upmsUserService.deleteByPrimaryKey(userId)>0){
            return R.ok();
        }
        return R.error();
    }
    /**
     * 批量删除用户
     * @param userIds
     * @return
     */
    @ApiOperation(value="批量删除用户")
    @PostMapping( "/batchRemove")
    @ResponseBody
    public R remove(@RequestParam("ids[]") Integer[] userIds){
        upmsUserService.batchRemove(userIds);
        return R.ok();
    }

    @PostMapping("/deleteUserRoleInfo")
    @ResponseBody
    public R deleteUserRoleInfo(Integer roleId, Integer userId) {
        if(upmsRoleService.deleteUserRoleInfo(roleId, userId) > 0) {
            return R.ok();
        }
        return R.error("删除角色的用户失败");
    }

    @PostMapping("/batchRemoveRole")
    @ResponseBody
    public R batchRemoveRole(@RequestParam("roleId") Integer roleId,
                             @RequestParam("ids[]") Integer[] userIds) {
        try {
            int count = upmsRoleService.batchRemoveRole(roleId, userIds);
            if(count>0) {
                return R.ok("删除" + count + "条记录！");
            }
            return R.error("批量删除失败！");
        }catch (RuntimeException e) {
            return R.error();
        }
    }



}